package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /*
     * 新增菜品
     * */
    @Transactional//添加事物
    @Override
    public void addDish(DishDTO dishDTO) {
        //创建Dish对象
        Dish dish = new Dish();
        //拷贝对应数据
        BeanUtils.copyProperties(dishDTO, dish);

        //补全数据
        if (dish.getStatus() != 1) {
            dish.setStatus(0);
        }
        //上传菜品信息
        dishMapper.insert(dish);
        //获取口味数据
        List<DishFlavor> list = dishDTO.getFlavors();
        //如果口味数据不为空则获取id并将id添加到dishFlavor中后上传
        if (list != null && list.size() > 0) {
            //获取该菜品id
            Long dishId = dishMapper.selsctId(dish.getName());
            for (DishFlavor flavor : list) {
                flavor.setDishId(dishId);
                //将口味数据添加到数据库
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    /*
     * 菜品分页查询
     * */
    @Override
    public PageResult getDishPage(DishPageQueryDTO dishPageQueryDTO) {
        //
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> data = dishMapper.selectPage(dishPageQueryDTO);
        //封装并返回
        return new PageResult(data.getTotal(), data.getResult());
    }

    /*
     * 菜品批量删除
     * */
    @Transactional//开启事物保证一致性
    @Override
    public void deleteDish(List<Long> ids) {
        for (Long id : ids) {
            //1.判断菜品是否可以删除-获取菜品信息
            Dish dish = dishMapper.getById(id);
            //判断是否禁用
            if (dish.getStatus() == StatusConstant.ENABLE) {
                //启用状态无法删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }

        }//判断是否关连套餐
        List<Long> setmealId = setmealDishMapper.getByIds(ids);
        if (setmealId!=null && setmealId.size()>0) {
            //存在套餐关联菜品无法删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //2.删除可以删除的菜品
        //删除菜品
        dishMapper.deleteByIds(ids);
        //删除菜品口味相关数据
        dishFlavorMapper.deleteByDishIds(ids);
    }
    /*
     * 根据id查询菜品信息
     * */
    @Override
    public DishVO getByIdFlavor(Long id) {
        //获取dish信息,封装到dishvo对象当中
        Dish dish = dishMapper.selsctById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        //获取口味信息
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectById(id);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }
    /*
     * 更新菜品信息
     * */
    @Transactional
    @Override
    public void updateDish(DishDTO dishDTO) {
        //新建Dish对象储存相应数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //更新菜品信息
        dishMapper.updateDish(dish);
        //更新菜品相关口味信息
        //删除原有菜品口味信息
        //获取该菜品id
        List<Long> ids = new ArrayList<>();
        ids.add(dish.getId());
        dishFlavorMapper.deleteByDishIds(ids);
        //获取口味数据
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();

        //如果口味数据不为空则将新的数据上传
        if (dishFlavors != null && dishFlavors.size()>0) {
            dishFlavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }
    /*
    * 起售/停售菜品
    * */
    @Override
    public void updateDishStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.updateDish(dish);
    }

    /*
    * 根据分类查询菜品
    * */
    @Override
    public List<DishVO> getDishList(Dish dish) {
        //定义集合用来接收返回的数据
        List<DishVO> dishVOList = new ArrayList<>();
        //获取所有的dish数据
        List<Dish> dishList = dishMapper.selsctDishListByCategoryId(dish);
        //根据每个dish的id获取对应口味信息
        for (Dish d : dishList) {
            //定义dishvo对象拷贝数据
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);
            //获取口味信息
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectById(d.getId());
            //将获取到的结果存储到vo对象当中
            dishVO.setFlavors(dishFlavors);
            //将vo对象存储到vo集合当中
            dishVOList.add(dishVO);
        }
        //返回结果
        return dishVOList;
    }

    @Override
    public List<Dish> getDishList2(Dish dish) {
        return dishMapper.selsctDishListByCategoryId(dish);
    }
}
